package com.zr.command.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.annotation.security.PermitAll;
import javax.servlet.http.HttpServletResponse;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.zr.common.core.domain.entity.SysOrganization;
import com.zr.common.core.domain.entity.SysUser;
import com.zr.common.utils.SecurityUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.zr.common.annotation.Log;
import com.zr.common.core.controller.BaseController;
import com.zr.common.core.domain.AjaxResult;
import com.zr.common.enums.BusinessType;
import com.zr.command.domain.EimCommandSchedule;
import com.zr.command.service.IEimCommandScheduleService;
import com.zr.common.utils.poi.ExcelUtil;
import com.zr.common.core.page.TableDataInfo;

/**
 * 指挥调度工单Controller
 * 
 * @author chengkai
 * @date 2022-10-21
 */
@RestController
@RequestMapping("/eim/schedule")
public class EimCommandScheduleController extends BaseController
{
    @Resource
    private IEimCommandScheduleService eimCommandScheduleService;

    /**
     * 查询指挥调度工单列表
     */
    @PreAuthorize("@ss.hasPermi('eim:schedule:list')")
    @GetMapping("/list")
    public TableDataInfo list(EimCommandSchedule eimCommandSchedule)
    {
        startPage();
        List<EimCommandSchedule> list = eimCommandScheduleService.selectEimCommandScheduleList(eimCommandSchedule);
        return getDataTable(list);
    }

    /**
     * 查询隔离点列表
     */
    @GetMapping("/listQuarantineAddress")
    public AjaxResult listQuarantineAddress()
    {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        List<String> list = eimCommandScheduleService.listQuarantineAddress(user.getOrganizationId());
        return AjaxResult.success(list);
    }

    /**
     * 查询指挥调度工单列表
     */
    @PermitAll
    @PostMapping("/listSchedule")
    public TableDataInfo listSchedule(@RequestBody String json)
    {
        EimCommandSchedule eimCommandSchedule= JSON.parseObject(json,EimCommandSchedule.class);
        SysUser user = SecurityUtils.getLoginUser().getUser();
        startPage();
        List<EimCommandSchedule> list = eimCommandScheduleService.listEimCommandSchedule(eimCommandSchedule,user);
        return getDataTable(list);
    }

    /**
     * 导出指挥调度工单列表
     */
    @PreAuthorize("@ss.hasPermi('eim:schedule:export')")
    @Log(title = "指挥调度工单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, EimCommandSchedule eimCommandSchedule)
    {
        List<EimCommandSchedule> list = eimCommandScheduleService.selectEimCommandScheduleList(eimCommandSchedule);
        ExcelUtil<EimCommandSchedule> util = new ExcelUtil<EimCommandSchedule>(EimCommandSchedule.class);
        util.exportExcel(response, list, "指挥调度工单数据");
    }

    /**
     * 获取指挥调度工单详细信息
     */
    @PreAuthorize("@ss.hasPermi('eim:schedule:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        EimCommandSchedule eimCommandSchedule=eimCommandScheduleService.selectEimCommandScheduleById(id);
        System.out.println("getinfo:"+eimCommandSchedule);
        return AjaxResult.success(eimCommandSchedule);
    }

    /**
     * 新增指挥调度工单
     */
    @PreAuthorize("@ss.hasPermi('eim:schedule:add')")
    @Log(title = "指挥调度工单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody EimCommandSchedule eimCommandSchedule)
    {
        System.out.println(eimCommandSchedule);
        return toAjax(eimCommandScheduleService.insertEimCommandSchedule(eimCommandSchedule));
    }

    /**
     * 修改指挥调度工单
     */
    @PreAuthorize("@ss.hasPermi('eim:schedule:edit')")
    @Log(title = "指挥调度工单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody EimCommandSchedule eimCommandSchedule)
    {
        return toAjax(eimCommandScheduleService.updateEimCommandSchedule(eimCommandSchedule));
    }

    /**
     * 删除指挥调度工单
     */
    @PreAuthorize("@ss.hasPermi('eim:schedule:remove')")
    @Log(title = "指挥调度工单", businessType = BusinessType.DELETE)
	@DeleteMapping("/delete")
    public AjaxResult remove(@RequestBody EimCommandSchedule eimCommandSchedule)
    {
        return toAjax(eimCommandScheduleService.deleteEimCommandScheduleById(eimCommandSchedule));
    }

    /**
     * 分配指挥调度工单
     */
    @PostMapping("/dispatchSchedule")
    public AjaxResult dispatchSchedule(@RequestBody String data)
    {
        JSONObject object=JSON.parseObject(data);
        List<Long> ids = object.getList("ids",Long.class);
        Long groupId=object.getLong("groupId");
        return toAjax(eimCommandScheduleService.dispatchScheduleById(ids,groupId));
    }

/**
     * 根据工单执行状态查找对应状态的工单数量
     */
    @GetMapping("/showProportion")
    public AjaxResult showStatusProportion(){
        SysUser user = SecurityUtils.getLoginUser().getUser();
        String[] status=new String[]{"未分配","待接收","进行中","已完成"};
        List<Map<String, Object>> list=new ArrayList<>();
        for(int i=0;i<4;i++){
            Map<String, Object> map=new HashMap<>();
            map.put("name",status[i]);
            int count=eimCommandScheduleService.selectScheduleCountByStatus(i,user.getUserName());
            map.put("value",count);
            list.add(map);
        }
        return AjaxResult.success(list);
    }

    /**
     * 根据工单类型查找对应的工单数量
     */
    @GetMapping("/showTypeProportion")
    public AjaxResult showTypeProportion(){
        SysUser user = SecurityUtils.getLoginUser().getUser();
        String[] type=new String[]{"病例流调","采样消杀","核酸检测","120转运"};
        List<Map<String, Object>> typeList=new ArrayList<>();
        for(int i=0;i<4;i++){
            Map<String, Object> map=new HashMap<>();
            map.put("name",type[i]);
            int count=eimCommandScheduleService.selectScheduleCountByType(i,user.getUserName());
            map.put("value",count);
            typeList.add(map);
        }
        return AjaxResult.success(typeList);
    }
}
